
//
// 120128 - AH - (Garage) Created.
// 120321 - AH - Removed @Override for interface method (1.5 compatible).
//

package org.soekrat.gui.swing.ptable;

import java.awt.Component;

import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

import org.soekrat.engine.ontology.ptable.Product;
import org.soekrat.engine.ontology.ptable.ProductTableFactor;

/**
 * <p>
 *   {@link TableCellEditor} for 
 *   {@link ProductTableView}'s table.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class ProductTableTableCellEditor extends AbstractCellEditor 
  implements TableCellEditor 
{
  private final JTextField m_field=new JTextField();
  
  private final ProductTableView m_ptv;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public ProductTableTableCellEditor(ProductTableView ptv)
  {
    m_ptv=ptv;
  }

  // Specified in TableCellEditor.
  public Object getCellEditorValue(){ return m_field.getText(); }

  // Specified in TableCellEditor.
  public Component getTableCellEditorComponent(JTable table, Object value,
    boolean isSelected, int row, int col) 
  {
    String txt=(value==null) ? "" : value.toString();
    
    m_ptv.setEdited(value, row, col);
    
    if (value instanceof Product)
    {
      txt=((Product)value).getSoekratName().getPure();
    }
    else if (value instanceof ProductTableFactor)
    {
      txt=((ProductTableFactor)value).getSoekratName().getPure();
    }
    
    m_field.setText(txt);
    
    return m_field;
  }

}
